<?php
// PXFInstance::breakPoint($current_categories);

class ControllerAdmin extends PXFControllerAdmin
{
	static $tablinks = null;

	public function __construct()
	{ 
		self::$tablinks = array(
			array('controller' => 'ajax_users', 'action' => 'index',  'title' => 'Listado de Usuarios'),
			array('controller' => 'ajax_users', 'action' => 'create', 'title' => 'Nuevo Usuario'),
		);
		parent::__construct();
		$this->tpl->assign('tablinks',self::$tablinks);
	}

	public function index()
	{
		PXFConfigAdmin::$site['page_title'] = 'Usuarios';
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->display('ajax_users.list.tpl');
	}
	
	public function user_save()
	{
    /* Required params */
	  $id = PXFRequest::reqGetOr404('id'); 
	  $pw = PXFRequest::reqGetOrDefault('password',false); 
	
	  /* Models */
	  $mod_users= PXFModel::getModel('user');
	  $user = $mod_users->find($id);
	  PXFRequest::notNullOr404($user); 

	  /* Validation. */
    $validator = PXFValidator::getNew();

	  $name = new ValidatorVoid('name',false);
	  $last = new ValidatorVoid('lastname',false);
	  $prof = new ValidatorVoid('profile',false);
	  $mail = new ValidatorVoid('email',false);
		$validator->addItem($name);
	  $validator->addItem($last);
	  $validator->addItem($prof);
	  $validator->addItem($mail);

		if($pw)
		{
	  	$pswd = new ValidatorVoid('password',false); 
	  	$validator->addItem($pswd);
		}

		$errors = $validator->validate();
		$user->name = $name->getValue();
		$user->mail = $mail->getValue();

		if($pw)
		{
			$user->pswd = md5($pswd->getValue());
		}

		$user->lastname = $last->getValue();
		$user->profile_id = $prof->getValue();
	
	  if(!$errors)
		{
			$user->save();
			PXFRequest::redirect('index.php?c=ajax_users');
		}
		pxf_json_encode(array('errors'=> $errors));
  }

	public function user()
	{
		$id = PXFRequest::reqGetOr404('id'); 

		$mod_users = PXFModel::getModel('user');
		$user = $mod_users->find($id);

		$mod_profiles = PXFModel::getModel('profile');
		$profiles = $mod_profiles->findAll();

		PXFConfigAdmin::$site['page_title'] = 'Editar Usuario';
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->assign('user',$user);
		$this->tpl->assign('profiles',$profiles);
		$this->tpl->display('ajax_users.interface.tpl');
	}

	public function create()
	{
	  $mod_users = PXFModel::getModel('user');
	  $user = $mod_users->getNew();
		$user->name = 'Nuevo Usuario';
		$user->lastname = '';
		$user->profile_id = 0; 
		$user->save();
		PXFRequest::redirect(sprintf('index.php?c=ajax_users&a=user&id=%s',$user->id));
	}

	public function user_delete()
	{
	  $id = PXFRequest::reqGetOr404('id'); 
	  $mod_users = PXFModel::getModel('user');
		$user= $mod_users->find($id);
		$user->delete();
		echo pxf_json_encode(array());
	}

	public function users_get_all()
	{
		$mod_users = PXFModel::getModel('user');
		$users = $mod_users->findAll();

		$result = array();
		foreach($users as $usr)
		{
			$result[] = array('lastname' => htmlentities($usr->lastname), 'name' => htmlentities($usr->name),'id' => $usr->id);
		}
		echo pxf_json_encode($result);
	}

}
?>
